<?php

class constraint {
	protected $conditions = array();
	protected $l, $op, $r;

	function __construct($array) {
		$count = count($array);
		if($count==3) {
			$this->l = $array[0];
			$this->op = $array[1];
			$this->r = $array[2];
		} else {
			$previous = $array[0];
			for($i=1; $i+1<$count; $i+=2) {
				$previous = new constraint(array($previous, $array[$i], $array[$i+1]));
			}
		}
	}

	public static function newConstraint($array) {
		if(is_object($array)) {
			return $array;
		}
		$count = count($array);
		if($count==1) {
			return new predicate($array[0][0], $array[0][1], $array[0][2]);
		} else if(!$array[0]) {
			return constraint::newConstraint($array[2]);
		} else {
			return new constraint($array);
		}
	}

	public function toSQL($model) {
		return $this->l->toSQL($model) . $this->op . $this->r->toSQL($model);
	}
}

?>
